<template>
    <div id="fleetManage" class="center_div">
        <div class="leftTreeClass">
            <div style="height: 100%;overflow:auto;margin-top: 2%">
                <el-tree check-strictly
                         :load="loadChildren"
                         highlight-current
                         lazy
                         node-key="id"
                         :props="defaultProps"
                         :filter-node-method="filterNode"
                         ref="tree"
                         @node-click="loadData">
                       <span class="custom-tree-node dataManageBtn24px" slot-scope="{ node, data }">
                    <span v-if="data.type===0"><img class="treeLableImg" src="@/assets/images/mapicon/tree_fleet.png"
                    >{{ node.label }}</span>
                    <span v-if="data.type===1"><img src="@/assets/images/mapicon/tree_fleet.png"
                                                    class="treeLableImg">{{ node.label }}</span>
                </span>
                </el-tree>
            </div>
        </div>
        <div style="float:right;width: 80%">
            <div class="button dataManage18px" style="margin-top: 1%;">
                <el-button type="primary" size="medium" @click="add"class="dataManageBtn22BtnpPx">添加部门</el-button>
            </div>
            <div >
              <el-table
                        :data="tableData"
                        style="width:100%;text-align: center;"
                        border
                        :header-cell-style="headerCellClassName"
                        cell-class-name="dataManage18px"
                >
                    <el-table-column align="center"  v-for="title in tableTitle" :key="title.id" :prop="title.prop"
                                     :label="title.label" :show-overflow-tooltip="title.overflowTooltip">
                    </el-table-column>
                    <template v-if="buttons && buttons.length!==0">
                        <el-table-column
                                class="align-right"
                                width="350"
                                header-align="center"
                                label="操作"
                                align="center" >
                            <template slot-scope="scope">
                                <el-button v-for="button in buttons"
                                           @click="operate(scope.row,button.href)" :type="button.type"
                                           class="el-button el-button--text el-button--medium dataManageBtn22px">
                                    {{button.label}}
                                </el-button>
                            </template>
                        </el-table-column>
                    </template>
                </el-table>
                <el-pagination
                        layout="total,prev, pager, next"
                        @current-change="changePageNum"
                        background
                        :current-page="pageNum"
                        :page-size="pageSize"
                        :total="total">
                </el-pagination>
            </div>
            <div>
                <el-dialog :title="title" :visible.sync="showBox" width="35%" append-to-body @close="closeDialog">
                    <el-form ref="form" :model="form" :rules="rules" label-width="120px" >
                        <el-row>
                            <el-form-item  label="基本信息"></el-form-item>
                        </el-row>
                        <el-row v-show="showAdd">
                            <el-col :span="12">
                                <el-form-item label="所属部门" prop="parentName">
                                    <el-input v-model="form.parentName" :disabled="true"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row v-show="showAdd">
                            <el-col >
                                <el-form-item label="部门名称" prop="fleetName">
                                    <el-input v-model="form.fleetName"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row v-show="!showAdd">
                            <el-col :span="12">
                                <el-form-item label="部门编号">
                                    <el-input v-model="form.fleetNum" :disabled="true"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row v-show="!showAdd">
                            <el-col >
                                <el-form-item label="部门名称" prop="fleetName">
                                    <el-input v-model="form.fleetName"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row v-if="false">
                            <el-col :span="12">
                                <el-form-item label="所属省" prop="pv">
                                    <el-select v-model="form.pv" placeholder="请选择" @change="getCityByProvince(form.pv)">
                                        <el-option
                                                v-for="item in provinceOptions"
                                                :key="item.id"
                                                :label="item.name"
                                                :value="item.id">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="所属市" prop="ct">
                                    <el-select v-model="form.ct" placeholder="请选择">
                                        <el-option
                                                v-for="item in cityOptions"
                                                :key="item.id"
                                                :label="item.name"
                                                :value="item.id">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button size="medium"  @click="closeBox"class="dataManageBtn22BtnpPx">取 消</el-button>
                        <el-button type="primary" size="medium" @click="addFleet" v-show="showAdd"class="dataManageBtn22BtnpPx">添 加</el-button>
                        <el-button type="primary" size="medium" @click="updateFleet" v-show="!showAdd"class="dataManageBtn22BtnpPx">修 改</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</template>

<script>
  import FleetManage from '@/api/dataManages/FleetManage';

  export default {
    name: 'FleetManage',
    props: {
      FleetId: Number,
      userId: Number,
    },
    data() {
      return {
        defaultProps: {
          label: 'name',
          isLeaf: 'isLeaf',
        },
        fleetName: '',
        searchData: {},
        tableTitle: [
          {id: 1, label: '部门名称', prop: 'NAME', overflowTooltip: true},
          {id: 2, label: '部门编号', prop: 'FleetNum', overflowTooltip: true},
          {id: 3, label: '部门创建时间', prop: 'CREATEDATETIME', overflowTooltip: true},
        ],
        tableData: [],
        total: 0,
        pageNum: 1,
        pageSize: 10,
        buttons: [{label: '添加子部门', href: 'add'}, {label: '编辑', href: 'update'}, {label: '删除', href: 'delete'}],
        provinceOptions: [],
        cityOptions: [],
        systemOptions: [],
        showBox: false,
        showAdd: true,
        title: '',
        form: {
          parentName: '',
          fleetId: '',
          fleetNum: '',
          fleetName: '',
          pv: '',
          ct: '',
          fleetSystem: [],
        },
        rules: {
          parentName: [
            {required: true, message: '请输入所属部门', trigger: 'blur'},
          ],
          fleetName: [
            {required: true, message: '请输入部门名称', trigger: 'blur'},
            {min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur'}
          ], fleetNum: [
            {required: true, message: '请输入部门编号', trigger: 'blur'},
          ],
          pv: [
            {required: true, message: '请选择所属省', trigger: 'change'},
          ], ct: [
            {required: true, message: '请选择所属市', trigger: 'change'},
          ],
        },
        checkedDispatch: [],
        DispatchLicense1: [
          {label: '语音', value: 1},
          {label: '地图', value: 2},
          {label: '视频', value: 3},
          {label: '会议', value: 4},
          {label: '会话', value: 5},
          {label: '数据统计', value: 6}],
        DispatchLicense2: [
          {label: '组派接', value: 7},
          {label: '动态重组', value: 8},
          {label: '调度员呼叫', value: 9},
          {label: '单呼', value: 10},
          {label: '组呼', value: 11},
          {label: '临时组呼', value: 12},
          {label: 'IP语音呼叫', value: 13},
          {label: 'IP视频呼叫', value: 14}],
        DispatchLicense3: [{label: '轨迹回放', value: 15}, {label: '框选呼叫', value: 16}, {label: '电子围栏', value: 17}],
        DispatchLicense4: [{label: '语音会议', value: 18}, {label: '视频会议', value: 19}],
        DispatchLicense5: [
          {label: '媒体管理', value: 20},
          {label: '统计查询', value: 21},
          {label: '业务管理', value: 22},
          {label: '系统管理', value: 23}],
        checkedApp: [],
        AppLicense1: [
          {label: '语音', value: 1},
          {label: '通讯', value: 2},
          {label: '消息', value: 3},
          {label: '地图', value: 4},
          {label: '工作台', value: 5}],
        AppLicense2: [
          {label: '图品回传', value: 6},
          {label: '录像回传', value: 7},
          {label: '指挥中心', value: 8},
          {label: '一件告警', value: 9},
          {label: '视频推送', value: 10}],
      };
    },
    created() {
      this.changeDmgPage();
    },
    updated() {
    },
    mounted() {
      this.getSystem();
      this.query();
      this.getProvince();
    },
    methods: {
      changeDmgPage(){
        let w = window.screen.width;
        let h = window.screen.height;
        if (w == 1920) {
          if (h == 1080) {
          } else {
            this.pageSize = 12
          }
        }
      },
      //表头样式
      headerCellClassName() {
        return 'font-size:18px;color:#515A6E;padding:10px 0px 10px 0px !important;background:#F5F5F6;border:1px solid #EBEBEB';
      },
      //表格样式
      cellStyleClassName() {
        return 'font-size:13px;text-align:center;color:#606266;padding:5px 0px 5px 0px !important;border:1px solid #EBEBEB';
      },
      refreshNodeBy: function(id) {
        var node = this.$refs.tree.getNode(id);
        node.loaded = false;
        node.expand();
      },
      handleCheckedDispatchChange: function(value) {
        var checkedCount = value.length;
      },
      getCityByProvince: function(key) {
        this.form.ct = '';

        FleetManage.getCityByProvince({pv: key}).then(res => {
          // 返回数据
          var data = res;
          if (res.success) {
            this.cityOptions = data.obj.list;
          } else {
            this.$message(res.msg);
          }
        }).catch(err => {
          // 异常情况
          console.log(err);
        });
      },
      getProvince: function() {
        FleetManage.getProvince({}).then(res => {
          // 返回数据
          var data = res;
          if (res.success) {
            this.provinceOptions = data.obj.list;
          } else {
            this.$message(res.msg);
          }
        }).catch(err => {
          // 异常情况
          console.log(err);
        });
      },
      getSystem: function() {
        FleetManage.getSystem({}).then(res => {
          // 返回数据
          // var data = res;
          //   if (res.success) {
          //     this.systemOptions = data.obj.list;
          //   } else {
          //     this.$message(res.msg);
          //   }
          if (res.success) {
            this.systemOptions = res.obj.list;
            this.form.fleetSystem = [];
            for (var i = 0; i < res.obj.list.length; i++) {
              this.form.fleetSystem.push(res.obj.list[i].System_ID);
            }
            console.log('this.form.fleetSystem');
            console.log(this.form.fleetSystem);
          } else {
            this.$message(res.msg);
          }

        }).catch(err => {
          // 异常情况
          console.log(err);
        });
      },
      loadChildren(node, resolve) {
        if (node.data) {
          this.getChild(node.data.id, node.data.type, resolve);
        } else {
          setTimeout(() => { this.getChild(this.FleetId, 0, resolve);}, 0);
        }
      },
      getChild: function(id, type, resolve) {
        var jsonData = {
          parentId: id,
          type: type,
        };
        FleetManage.getFleet(jsonData).then(res => {
          // 返回数据
          res = res;
          if (res.success) {
            var childNodes = [];
            if (res.obj) {
              childNodes = res.obj;
            }
            resolve(childNodes);
          }
        }).catch(err => {
          // 异常情况
          console.log(err);
        });

      },
      filterNode: function(value, data) {
        if (!value)
          return true;
        else
          return data.name.indexOf(value) !== -1;
      },
      timeFormat2(time) {
        var date, Y, M, D, h, m, s;
        date = new Date(time);
        Y = date.getFullYear() + '年';
        M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '月';
        D = (date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate()) + '日';
        h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + '时';
        m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + '分';
        s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())+ '秒';;
        return Y + M + D + h + m + s;
      },
      timeFormat(time) {
        var date, Y, M, D, h, m, s;
        date = new Date(time);
        Y = date.getFullYear() + '-';
        M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        D = (date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate()) + ' ';
        h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
        m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
        s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
        return Y + M + D + h + m + s;
      },
      loadData: function(node) {
        this.fleetName = node.name;
        this.searchData.fleetName = node.name;
        this.searchData.fleetId = node.id;
        var data = {
          fleetId: node.id,
          pageNum: 1,
          pageSize: this.pageSize,
        };
        FleetManage.getAllFleet(data).then(res => {
          // 返回数据
          var data = res;
          if (res.success) {
            for (var i = 0; i < data.obj.list.length; i++) {
              data.obj.list[i].CREATEDATETIME = this.GlobalFunc.timeFormatRow(data.obj.list[i].CREATEDATETIME);
            }
            this.tableData = data.obj.list;
            this.total = data.obj.total;
          } else {
            this.$message(res.msg);
          }

        }).catch(err => {
          // 异常情况
          console.log(err);
        });
      },
      query: function(pageNum) {
        var data = {
          fleetId: this.searchData.fleetId || '',
          pageNum: pageNum || this.pageNum,
          pageSize: this.pageSize,
        };
        if (data.fleetId === '') {
          console.log('当前调度台id');
          data.fleetId = this.FleetId;
        }
        FleetManage.getAllFleet(data).then(res => {
          // 返回数据
          var data = res;
          if (res.success) {
            for (var i = 0; i < data.obj.list.length; i++) {
              data.obj.list[i].CREATEDATETIME = this.GlobalFunc.timeFormatRow(data.obj.list[i].CREATEDATETIME);
            }
            this.tableData = data.obj.list;
            this.total = data.obj.total;
          } else {
            this.$message(res.msg);
          }

        }).catch(err => {
          // 异常情况
          console.log(err);
        });
      },
      formClear: function() {
        this.form.parentName = '';
        this.form.fleetId = '';
        this.form.fleetNum = '';
        this.form.fleetName = '';
        this.form.pv = '';
        this.form.ct = '';
        this.checkedDispatch = [];
        this.checkedApp = [];
      },
      add: function() {
        var that = this;
        that.formClear();
        that.showBox = true;
        that.showAdd = true;
        that.title = '添加部门';
        that.setParentName();
        that.cityOptions = [];
      },
      closeDialog() {
        this.$nextTick(() => {
          this.$refs['form'].resetFields();
        });
      },
      addFleet: function() {
        var validate = false;
        this.$refs['form'].validate((valid) => {
          if (valid) {
            validate = true;
          } else {
            console.log('error submit!!');
            validate = false;
            return false;
          }
        });
        if (validate === false) {
          return;
        }
        var that = this;
        var data = {
          fleetName: that.form.fleetName,
          pv: that.form.pv,
          ct: that.form.ct,
          fleetSystem: that.form.fleetSystem.join(','),
        };
        if (that.title === '添加部门') {
          data.fleetId = this.FleetId;
        } else if (that.title === '添加子部门') {
          data.fleetId = that.form.fleetId;
        }
        var auditData = {
          dcId: that.$store.getters.userAccount,
          type: 5,
          status: '成功',
          describe: that.$store.getters.userName + '添加部门:' + data.fleetName,
          time: new Date,
          fleetId: that.$store.getters.scsFleetId,
          detailType: 23,
          remark: '',
        };

        FleetManage.addFleet(data).then(res => {
          // 返回数据
          var res = res;
          if (res.success) {
            this.GlobalFunc.GlaAddAudit(auditData);
            that.closeBox();
            that.$message.success('添加成功');
            that.refreshNodeBy(this.FleetId);
            that.query();
          } else {
            auditData.status = '失败';
            this.GlobalFunc.GlaAddAudit(auditData);
            this.$message(res.msg);
          }
        }).catch(err => {
          // 异常情况
          auditData.status = '异常';
          this.GlobalFunc.GlaAddAudit(auditData);
          console.log(err);
        });
      },
      updateFleet: function() {
        var validate = false;
        this.$refs['form'].validate((valid) => {
          if (valid) {
            validate = true;
          } else {
            console.log('error submit!!');
            validate = false;
            return false;
          }
        });
        if (validate === false) {
          return;
        }
        var that = this;
        var data = {
          fleetName: that.form.fleetName,
          pv: that.form.pv,
          ct: that.form.ct,
          fleetId: that.form.fleetId,
          fleetSystem: that.form.fleetSystem.join(','),
        };
        var auditData = {
          dcId: that.$store.getters.userAccount,
          type: 5,
          status: '成功',
          describe: that.$store.getters.userName + '修改部门:' + data.fleetName,
          time: new Date,
          fleetId: that.$store.getters.scsFleetId,
          detailType: 23,
          remark: '',
        };
        FleetManage.updateFleet(data).then(res => {
          // 返回数据
          if (res.success) {
            this.GlobalFunc.GlaAddAudit(auditData);
            that.closeBox();
            that.$message.success('编辑成功');
            that.refreshNodeBy(this.FleetId);
            that.query();
          } else {
            auditData.status = '失败';
            this.GlobalFunc.GlaAddAudit(auditData);
            this.$message(res.msg);
          }
        }).catch(err => {
          //异常情况
          auditData.status = '异常';
          this.GlobalFunc.GlaAddAudit(auditData);
          console.log(err);
        });
      },
      setParentName: function() {
        var that = this;
        var jsonData = {
          parentId: this.FleetId,
          type: 0,
        };
        FleetManage.getFleet(jsonData).then(res => {
          if (res.success) {
            that.form.parentName = res.obj[0].name;
          }
        }).catch(err => {
          // 异常情况
          console.log(err);
        });
      },
      operate: function(row, href) {
        var that = this;
        that.formClear();
        if (href.indexOf('add') !== -1) {
          that.showBox = true;
          that.showAdd = true;
          that.title = '添加子部门';
          that.form.parentName = row.NAME;
          that.form.fleetId = row.FLEET_ID;
        } else if (href.indexOf('update') !== -1) {
          that.showBox = true;
          that.showAdd = false;
          that.title = '编辑部门';
          that.form.fleetId = row.FLEET_ID;
          that.form.fleetNum = row.FleetNum;
          that.form.fleetName = row.NAME;
          that.form.pv = row.PV_ID;
          that.getCityByProvince(row.PV_ID);
          that.form.ct = row.CT_ID;
          that.form.fleetSystem = row.fleetSystem.split(',');
          if (that.fleetName === '') {
            that.setParentName();
          } else {
            that.form.parentName = that.fleetName;
          }
        } else {
          var auditData = {
            dcId: that.$store.getters.userAccount,
            type: 5,
            status: '成功',
            describe: that.$store.getters.userName + '删除部门:' + row.NAME,
            time: new Date,
            fleetId: that.$store.getters.scsFleetId,
            detailType: 23,
            remark: '',
          };
          this.$confirm('是否确定删除', '', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
            customClass: 'message-delete'
          }).then(() => {

            FleetManage.deleteFleet({fleetId: row.FLEET_ID}).then(res => {
              if (res.success) {
                this.GlobalFunc.GlaAddAudit(auditData);
                that.$message.success('删除成功');
                that.refreshNodeBy(this.FleetId);
                that.pageNum = 1;
                that.query(1);
              } else {
                that.$message(res.msg);
                auditData.status = '失败';
                this.GlobalFunc.GlaAddAudit(auditData);
              }
            }).catch(err => {
              // 异常情况
              console.log(err);
              auditData.status = '异常';
              this.GlobalFunc.GlaAddAudit(auditData);
            });

          }).catch(() => {
          });

        }
      },
      closeBox: function() {
        this.formClear();
        this.showBox = false;
      },
      changePageNum: function(val) {
        this.pageNum = val;
        this.query();
      },
    },
  };
</script>

<style scoped>
    /deep/ .el-pagination__total {
        font-size: 18px;
    }
    /deep/ .el-dialog__header{
        /*border-bottom: 1px solid #cccccc!important;*/
    }
    /deep/ .el-form-item__label{
        font-size: 20px;
    }
    /deep/ .el-input__inner{
           font-size: 20px;
       }
    /deep/ .el-icon-close:before {
        font-size: 28px;
    }
    /deep/ .el-tree-node {
        margin-top: 25px !important;
        height: auto;
    }
    /*分页修改开始*/
    .el-pagination /deep/ .el-pagination__total {
        font-size: 20px;
        line-height: 60px;
    }.el-pagination /deep/ button /deep/ .el-icon .el-icon .el-icon-arrow-left {
        font-size: 50px;
             height: 60px;
             width: 40px;
    }
     /deep/ .el-pager li{
         margin-top: 15px;
         height: 60px;
         width: 60px;
         line-height: 60px;
         font-size: 18px;
     }
    /deep/  .el-pagination.is-background .btn-prev, .el-pager li{
        height: 60px;
        width: 60px;
    }/deep/ .el-pagination.is-background .btn-next, .el-pager li{
        height: 60px;
        width: 60px;
    }/deep/ .el-pagination .btn-prev  .el-icon{
        font-size: 40px;
    }/deep/ .el-pagination .btn-next .el-icon{
        font-size: 40px;
    }
    /deep/ .el-tooltip__popper{
        font-size: 25px;
    }
    /deep/ .el-icon-caret-right:before {
        font-size: 40px!important;
    }
     /*分页修改结束*/
    .leftTreeClass {
        float: left;
        width: 19%;
        height: 600px;
        background: white;
        position: absolute;
    }

    .treeLableImg {
        height: 25px;
        width: 25px;
        margin-right: 10px;
        vertical-align: middle
    }

    .el-dialog__body {
        height: 50vh;
        overflow: auto;
    }

    #fleetManage.el-checkbox__input > .el-checkbox__inner {
        display: none;
    }

    #fleetManage.is-leaf + .el-checkbox .el-checkbox__inner {
        display: inline-block;
    }


    /*优化start*/

    @media screen and (max-width: 1920px) and (max-height: 1280px) {
        .leftTreeClass {
            float: left;
            width: 19%;
            height: 1200px;
            background: #ffffff;
            position: absolute;
            border-right: 1px solid #EBEEF5;
        }

    }

    /deep/ .el-message-box__wrapper /deep/ .el-message-box /deep/ .el-message-box__header /deep/ .el-message-box__title {
        font-size: 50px!important;

    } /deep/ .el-message-box__wrapper /deep/ .el-message-box  /deep/ .el-message-box__content /deep/ .el-message-box__container /deep/ .el-message-box__status /deep/ .el-icon-warning /deep/ .el-message-box__message p {
        font-size: 50px!important;

    }
    /deep/ .el-message-box__title{
        font-size: 50px!important;
    }
    .message-delete{
        font-size: 50px!important;
    }

    /*优化end*/


</style>
<style>
    /*/deep/ .el-message-box__wrapper /deep/ .el-message-box /deep/ .el-message-box__header /deep/ .el-message-box__title {
        font-size: 50px!important;

    } /deep/ .el-message-box__wrapper /deep/ .el-message-box  /deep/ .el-message-box__content /deep/ .el-message-box__container /deep/ .el-message-box__status /deep/ .el-icon-warning /deep/ .el-message-box__message p {
          font-size: 50px!important;

      }*/


</style>
